package sk.tuke.magsa.personalistika.dao_impl;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
import sk.tuke.magsa.framework.CRUDDaoImpl;
import sk.tuke.magsa.framework.ValidatorException;
import sk.tuke.magsa.framework.ConnectionPool;
import sk.tuke.magsa.personalistika.entity.*;
//import sk.tuke.magsa.personalistika.entity.Oddelenie;
import sk.tuke.magsa.personalistika.dao.OddelenieDao;
import sk.tuke.magsa.personalistika.Application;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class OddelenieDaoImpl extends CRUDDaoImpl<Oddelenie> implements OddelenieDao {
    public OddelenieDaoImpl(ConnectionPool pool) {
        super(pool);
    }    

    protected PreparedStatement prepareInsertStatement(Connection connection, Oddelenie object) throws SQLException {
        String query = "INSERT INTO Oddelenie (nazov,kod,uroven) VALUES (?, ?, ?)";
        PreparedStatement pstm = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
        pstm.setString(1, object.getNazov());
        pstm.setString(2, object.getKod());
        pstm.setDouble(3, object.getUroven());
        test(object);
        return pstm;
    }

    protected PreparedStatement prepareUpdateStatement(Connection connection, Oddelenie object) throws SQLException {
                String query = "UPDATE Oddelenie SET nazov = ?,  kod = ?,  uroven = ?  WHERE ident = ?";
        PreparedStatement pstm = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
        pstm.setString(1, object.getNazov());
        pstm.setString(2, object.getKod());
        pstm.setDouble(3, object.getUroven());
        pstm.setInt(4, object.getIdent());

        return pstm;
    }

    protected PreparedStatement prepareDeleteStatement(Connection connection, Oddelenie object) throws SQLException {
        String query = "DELETE FROM Oddelenie WHERE nazov = ? AND kod = ? AND uroven = ? ";
        PreparedStatement pstm = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
        pstm.setString(1, object.getNazov());
        pstm.setString(2, object.getKod());
        pstm.setDouble(3, object.getUroven());

        return pstm;
    }

    protected PreparedStatement prepareFindStatement(Connection connection, Integer id) throws SQLException {
        String query = "SELECT * FROM Oddelenie WHERE ident = ? ";
        PreparedStatement pstm = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
        pstm.setInt(1, id);


        return pstm;

    }

    protected PreparedStatement prepareSelectStatement(Connection connection) throws SQLException {
         String query = "SELECT * FROM Oddelenie";
         PreparedStatement pstm = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
         return pstm;
         
    }

    protected Oddelenie createFromResultSet(ResultSet rs) throws SQLException {
        Oddelenie result = new Oddelenie();
        result.setIdent(rs.getInt("ident"));
        result.setNazov(rs.getString("nazov"));
        result.setKod(rs.getString("kod"));
        result.setUroven(rs.getDouble("uroven"));
        return result;
    }

    public List<Oddelenie> findString(String name) {
        List<Oddelenie> newList = new ArrayList<Oddelenie>();
        List<Oddelenie> list = super.selectAll();
        
        for (Oddelenie oddelenie : list )
        {
                if(oddelenie.getNazov().toString().contains(name))
                {
                    newList.add(oddelenie);
                    break;
                }
                if(oddelenie.getKod().toString().contains(name))
                {
                    newList.add(oddelenie);
                    break;
                }
                if(oddelenie.getUroven().toString().contains(name))
                {
                    newList.add(oddelenie);
                    break;
                }
                    }
            return newList;
    }

    public List<Oddelenie> findByProperty(String name, int i) {
        List<Oddelenie> newList = new ArrayList<Oddelenie>();
        List<Oddelenie> list = super.selectAll();
                for (Oddelenie oddelenie : list )
        {
            switch(i)
            {
                case 0:if(oddelenie.getNazov().contains((String)name))
                {
                    newList.add(oddelenie);
                    break;
                }
            break;
                case 1:if(oddelenie.getKod().contains((String)name))
                {
                    newList.add(oddelenie);
                    break;
                }
            break;
                case 2:if(oddelenie.getUroven() == Double.valueOf((String)name))
                {
                    newList.add(oddelenie);
                    break;
                }
            break;
                        }
        }
        return newList;
    }

    @Override
    protected void test(Oddelenie object) {
     
//-------------------------------Required-------------------------------
        if(object.getNazov() == null) {
             throw new ValidatorException("Property 'Nazov' is required!");
            }
    
//-------------------------------LENGTH-------------------------------
        if(object.getNazov() != null) {
            if( object.getNazov().length() > 30 || object.getNazov().length() < 5) {
                throw new ValidatorException("Property 'Nazov' has length out of range!");
            }
        }
     
//-------------------------------Required-------------------------------
        if(object.getKod() == null) {
             throw new ValidatorException("Property 'Kod' is required!");
            }
    
//-------------------------------LENGTH-------------------------------
        if(object.getKod() != null) {
            if( object.getKod().length() > 4 || object.getKod().length() < 1) {
                throw new ValidatorException("Property 'Kod' has length out of range!");
            }
        }
 


    }

    
}
